package com.wdc.sort;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@SuppressWarnings("all")
public class E01Leetcode1122 {
    public static void main(String[] args) {
        int[] arr1 = {3,2,1,1,4,8,6};
        int[] arr2 = {3,2,1};

        int[] ints = relativeSortArray(arr1, arr2);

        System.out.println(Arrays.toString(ints));


    }
    public static int[] relativeSortArray(int[] arr1, int[] arr2) {
       int[] arr = new int[1001];
        for (int i : arr1) {
            arr[i]++;
        }
        System.out.println(Arrays.toString(arr));

        //根据 arr2 里面的数据来取 放进新数组
        int[] result = new int[arr1.length];
        int k = 0;

        for (int i : arr2) {
            while (arr[i] > 0){
                result[k++] = i;
                arr[i]--;
            }
        }

        //处理尾巴上的数据
        for (int i = 0; i < arr.length; i++) {
            while (arr[i] > 0){
                result[k++] = i;
                arr[i]--;
            }
        }

        return result;

    }
}
